package com.sxkj.gis.wms.getCapabilities.controller;

import org.geotools.filter.Capabilities;
import org.geotools.filter.capability.FilterCapabilitiesImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class GetCapabilitiesController {
	
	@RequestMapping(value="WMS_getCapabilities.url.do")
	public void getCapabilities(){
		
		//test1
		/*URL url = null;
		try {
		  url = new URL("http://atlas.gc.ca/cgi-bin/atlaswms_en?VERSION=1.1.1&Request=GetCapabilities&Service=WMS");
		} catch (MalformedURLException e) {
		  //will not happen
			e.printStackTrace();
		}

		WebMapServer wms = null;
		try {
		  wms = new WebMapServer(url);
		  WMSCapabilities capabilities = wms.getCapabilities();

		  String serverName = capabilities.getService().getName();
		  String serverTitle = capabilities.getService().getTitle();
		  System.out.println("Capabilities retrieved from server: " + serverName + " (" + serverTitle + ")");
		} catch (IOException e) {
		  //There was an error communicating with the server
		  //For example, the server is down
			e.printStackTrace();
		} catch (ServiceException e) {
		  //The server returned a ServiceException (unusual in this case)
			e.printStackTrace();
		} catch (SAXException e) {
		  //Unable to parse the response from the server
		  //For example, the capabilities it returned was not valid
			e.printStackTrace();
		}*/
		
		//test2
		/*try{
			
			URL serverURL = new URL("http://atlas.gc.ca/cgi-bin/atlaswms_en?VERSION=1.1.1&Request=GetCapabilities&Service=WMS");
			
			WebMapServer wms = new WebMapServer(serverURL);
			
			WMSCapabilities capabilities = wms.getCapabilities();
			
			//Get formats for GetMap operation
			String formats = wms.getCapabilities().getRequest().getGetMap().toString();
			System.out.println(formats);
		}catch(Exception e){
			e.printStackTrace();
		}*/
		
		/*try{
		URL serverURL = new URL("http://easymap.shuxikeji.com:8080/geoserver/wms?request=getCapabilities");
		
		WebMapServer wms = new WebMapServer(serverURL);
		
		WMSCapabilities capabilities = wms.getCapabilities();
		
		//服务层
		System.out.println("");
		System.out.println("");
		System.out.println("--------------------------服务层相关信息---------------------------------");
		String serverName = capabilities.getService().getName();
		String serverTitle = capabilities.getService().getTitle();
		System.out.println("serverName：" + serverName );
		System.out.println("serverTile：" + serverTitle );
		System.out.println("abstract:"+capabilities.getService().get_abstract());
		System.out.println("abstract:"+capabilities.getService().getKeywordList());
		
		//responsibleParty
		ResponsibleParty responsibleParty = capabilities.getService().getContactInformation();
		System.out.println("OrganisationName:"+responsibleParty.getOrganisationName());;
		System.out.println("PositionName:"+responsibleParty.getPositionName());
		System.out.println("IndividualName:"+responsibleParty.getIndividualName());
		Contact contact = responsibleParty.getContactInfo();
		Address  address = contact.getAddress();
		System.out.println("adrressCity:"+address.getCity());
		System.out.println("adrressAdministrativeArea:"+address.getAdministrativeArea());
		System.out.println("adrressElectronicMailAddresses:"+address.getElectronicMailAddresses());
		
		//dimension
		java.awt.Dimension dimension =  capabilities.getService().getMaxDimension();
		System.out.println(dimension.getHeight()+":"+dimension.getWidth());
		System.out.println(dimension.height+":"+dimension.width);
		
		//url
		URL u = capabilities.getService().getOnlineResource();
		System.out.println("OnlineResource:"+u.toExternalForm());
		System.out.println("Protocol:"+u.getProtocol());
		System.out.println("Path:"+u.getPath());
		System.out.println("Query:"+u.getQuery());
		
		
		
		
		//请求层
		System.out.println("");
		System.out.println("");
		System.out.println("--------------------------请求层相关信息---------------------------------");
		List<String> formats = capabilities.getRequest().getGetMap().getFormats();
		for(int k=0;k<formats.size();k++){
			//打印支持格式：
			System.out.println("-------"+formats.get(k));
		}
		
		//获取最顶层，他将包含所有其他层
		System.out.println("");
		System.out.println("");
		System.out.println("--------------------------父层相关信息---------------------------------");
		Layer rootLayer = capabilities.getLayer();
		
		System.out.println("rootlayerName:"+rootLayer.getName());
		
		Map<String, CRSEnvelope> srsMap = rootLayer.getBoundingBoxes();
		Iterator<Map.Entry<String, CRSEnvelope>> iterator = srsMap.entrySet().iterator();
		while(iterator.hasNext()){
			Map.Entry<String, CRSEnvelope> entry = iterator.next();
			System.out.print("key:"+entry.getKey()+"   value:"+entry.getValue());
			CRSEnvelope crs = entry.getValue();
			System.out.println("  "+crs.getEPSGCode());
		}
		
		System.out.println("");
		System.out.println("--------------------------子层相关信息---------------------------------");
		Layer[] layerChild = rootLayer.getChildren();
		System.out.println("layerChildCount:"+layerChild.length);//子层个数
		System.out.println("--------------------------子层相关信息--EPSG编码-----------------------");
		Set<String> srschild = layerChild[0].getSrs();
		Iterator<String> itachild = srschild.iterator();  
		int p=0;
		while (itachild.hasNext()) {  
		  String str = itachild.next(); 
		  if(p%8==0){
			  System.out.println("");
		  }
		  System.out.print(str);
		  p++;
		} 
		System.out.println("");
		System.out.println("--------------------------子层样式等相关信息-----------------------");
		for(int i=0; i< layerChild.length; i++){
			  // Print layer info
			  System.out.println("Layer: ("+i+")"+layerChild[i].getName());
			  System.out.println("       "+layerChild[i].getTitle());
			  System.out.println("       "+layerChild[i].getChildren().length);
			  System.out.println("       "+layerChild[i].getBoundingBoxes());
			  CRSEnvelope env = layerChild[i].getLatLonBoundingBox();
			  System.out.println("  shenme? "+env.getLowerCorner()+" x "+env.getUpperCorner());

			  // Get layer styles
			  List styles = layerChild[i].getStyles();
			  for (Iterator it = styles.iterator(); it.hasNext();) {
			      StyleImpl elem = (StyleImpl) it.next();

			      // Print style info
			      System.out.println("Style:");
			      System.out.println("  Name:"+elem.getName());
			      System.out.println("  Title:"+elem.getTitle());
			      URL url =  elem.getStyleURL();
			  }
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		*/
		Capabilities capabilities = new Capabilities();
		FilterCapabilitiesImpl  fileter = 	capabilities.getContents();
		fileter.getIdCapabilities();
	
	}
	
}
